"""
@name : 前十ip地址.py
@author : wangshenghu
@projectname: 王生虎
@Date  : 2022/3/6
"""
import random


with open("ips.txt", "w+") as fp:
    # 生成120000行ip地址
    for i in range(12000):
        num1 = random.randint(1, 255)
        num2 = "172.25.254." + str(num1) + "\n"
        fp.write(num2)
    # 将光标移到文本开头处
    fp.seek(0)
    dict1 = {}
    for ip in fp:
        # 去除首尾的\n符号
        ip = ip.strip()
        # 将ip地址写进字典dict1
        if ip in dict1:
            dict1[ip] += 1
        else:
            dict1[ip] = 1
        #dict1[ip] = dict1.get(ip, 0) + 1
    top = 10
    print("出现频率前十的ip地址：")
    # 取字典值最大的前10个键值
    #print(dict1.items())
    print(sorted(dict1.items(), key=lambda x: x[1], reverse=True)[:top])
